//
//  HProductContentHeaderView.swift
//  StarTimesON
//
//  Created by Toj on 7/17/23.
//  Copyright © 2023 StarTimes. All rights reserved.
//

import UIKit

protocol HProductContentHeaderViewDelegate: NSObjectProtocol {
    func headerViewDidTapClose(_ headerView: HProductContentHeaderView)
    
    func headerViewDidTapGetVIP(_ headerView: HProductContentHeaderView)
    
    func headerViewDidTapSignin(_ headerView: HProductContentHeaderView)
    func headerViewDidTapMobile(_ headerView: HProductContentHeaderView)
    func headerViewDidTapTV(_ headerView: HProductContentHeaderView)
}

extension HProductContentHeaderViewDelegate {
    func headerViewDidTapClose(_ headerView: HProductContentHeaderView) { }
    
    func headerViewDidTapGetVIP(_ headerView: HProductContentHeaderView) { }
    
    func headerViewDidTapSignin(_ headerView: HProductContentHeaderView) { }
    func headerViewDidTapMobile(_ headerView: HProductContentHeaderView) { }
    func headerViewDidTapTV(_ headerView: HProductContentHeaderView) { }
}

class HProductContentHeaderView: UIView {
    
    weak var delegate: HProductContentHeaderViewDelegate?
    
    var selectedTV = false { didSet {
        productHeaderView.platformView.selectedTV = selectedTV
    } }
    
    private(set) var admobModel: STAdmobModel? { didSet {
        rewardHeaderView.isHidden = admobModel == nil
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    /* 激励广告 */
    private lazy var rewardHeaderView: HRewardHeaderView = {
        let view = HRewardHeaderView()
        view.delegate = self
        return view
    }()
    
    /* 商品导航 */
    private lazy var productHeaderView: HProductHeaderView = {
        let view = HProductHeaderView()
        view.isHidden = true
        // 登录按钮
        view.accountInfoView.signinButton.addTarget(self, action:#selector(didTapSignin), for:.touchUpInside)
        
        // 平台切换
        view.platformView.delegate = self
        return view
    }()
        
    /* 关闭 */
    private(set) lazy var closeButton: UIButton = {
        let view = UIButton()
        view.setImage("player_product_close".to_image, for: .normal)
        view.addTarget(self, action: #selector(didTapClose), for: .touchUpInside)
        return view
    }()
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension HProductContentHeaderView {
    
    func didTapClose() {
        delegate?.headerViewDidTapClose(self)
    }
    
    func didTapSignin() {
        delegate?.headerViewDidTapSignin(self)
    }
}

// MARK: - Public
extension HProductContentHeaderView {
    
    func show(admobModel: STAdmobModel?, rewardView isAnimate: Bool = false) {
        self.admobModel = admobModel
        
        // 设置广告上的信息: GETVIP默认文本, 定时器, 自动播放信息
        rewardHeaderView.show(admobModel: admobModel)
        // 设置商品平台信息, 个人信息, 商品信息
        
        productHeaderView.show(admobModel: admobModel)
        
        guard admobModel != nil else { return }
        
        guard isAnimate else {
            let viewWidth = frame.width
            rewardHeaderView.frame = CGRectMake(0, 0, viewWidth, 120)
            return
        }
        showAnimateRewardView()
    }
    
    func hiddenReward() {
        admobModel = nil
        productHeaderView.hidden()
    }
}

// MARK: - Private
private extension HProductContentHeaderView {
    
    func showAnimateRewardView() {
        closeButton.alpha = 0
        let viewWidth = frame.width
        var rewardFrame = rewardHeaderView.frame
        rewardFrame.origin.x = viewWidth
        rewardFrame.size.width = viewWidth
        rewardHeaderView.frame = rewardFrame
        
        rewardFrame.origin.x = 0
        UIView.animate(withDuration: 0.25) {
            self.closeButton.alpha = 1
            self.rewardHeaderView.frame = rewardFrame
        } completion: { _ in
        }
    }
}

// MARK: - 代理: HRewardHeaderViewDelegate
extension HProductContentHeaderView: HRewardHeaderViewDelegate {
    
    func rewardHeaderViewDidTapGetVIP(_ headerView: HRewardHeaderView) {
        delegate?.headerViewDidTapGetVIP(self)
        delegate?.headerViewDidTapClose(self)
    }
}

// MARK: - 代理: HRewardHeaderViewDelegate
extension HProductContentHeaderView: HPPlatformViewDelegate {
    
    func platformViewDidTapMobile(_ platformView: HPPlatformView) {
        delegate?.headerViewDidTapMobile(self)
    }
    
    func platformViewDidTapTV(_ platformView: HPPlatformView) {
        delegate?.headerViewDidTapTV(self)
    }
}

// MARK: - 初始化设置
private extension HProductContentHeaderView {
    
    func setupMakeAddSubviews() {
        addSubview(rewardHeaderView)
        addSubview(productHeaderView)
        addSubview(closeButton)
    }
    
    func setupMakeLayoutSubviews() {
        
        productHeaderView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(48)
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.top.equalTo(self)
            make.width.equalTo(50)
            make.height.equalTo(42)
        }
        
        rewardHeaderView.frame = CGRect(x: 0, y: 0, width: CGFloat.dv_width, height: 120)
    }
    
    func setupMakeInitialize() {
        backgroundColor = "#EFD494".to_color
    }
}
